package com.ljfiction.book.fragment.transactionrecord;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.fragment.BaseFragment;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.TimeTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by admin on 2017/9/22.
 */

public class TansetionDingyueFragment extends BaseFragment {


   private RecyclerView myshupingrecycler;

   private SmartRefreshLayout refreshMyshupingrecycle;
    private BaseQuickAdapter mAdapter;
    private List<DingyueBean.DataBean.ListBean> datas=new ArrayList<>();
    private DingyueBean dataBean;
    private int page=1;
    private LinearLayout layout;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myshuping_recycaler, null);
        myshupingrecycler = view.findViewById(R.id.myshupingrecycler);
        refreshMyshupingrecycle = view.findViewById(R.id.refresh_myshupingrecycle);
        layout=view.findViewById(R.id.zanwushuju);
        intData();
        intView();
        return view;
    }

   private void intView() {
        myshupingrecycler.setAdapter(mAdapter= new BaseQuickAdapter<DingyueBean.DataBean.ListBean, BaseViewHolder>(R.layout.dingyue_item, datas) {
            @Override
            protected void convert(BaseViewHolder helper, DingyueBean.DataBean.ListBean item) {
                AutoUtils.autoSize(helper.itemView);
                if (item.getMoneyType()==1){
                    helper.setText(R.id.dingyue_num, "订阅 "+String.valueOf(item.getSubscriptionPrice()) + " 辣椒券 ");
                }else {
                    helper.setText(R.id.dingyue_num, "订阅 "+String.valueOf(item.getSubscriptionPrice()) + " 辣椒 ");
                }
              helper.setText(R.id.dingyue_time, TimeTool.getTimeAndChange(String.valueOf(item.getSubscriptionDateTime())))
                      .setText(R.id.dingyue_chaptername,"《"+item.getBookName()+"》  "+item.getBookChapterName());
            }
        });
        myshupingrecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        myshupingrecycler.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        refreshMyshupingrecycle.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (dataBean.getData().isIsLastPage()){
                    refreshlayout.finishLoadMore();
                    refreshMyshupingrecycle.setEnableLoadMore(false);
                }else {
                    page++;
                    intData();

                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                datas.clear();
                refreshMyshupingrecycle.setEnableLoadMore(true);
                intData();
            }
        });
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP+"/userSubscriptionRecord")

                .params("userid", String.valueOf(UserManageBean.userBean.getUserId()))
                .params("startpage",String.valueOf(page))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshMyshupingrecycle.finishLoadMore();
                        refreshMyshupingrecycle.finishRefresh();
                        Gson g=new Gson();
                        try {
                            dataBean=g.fromJson(s,DingyueBean.class);
                            if (dataBean.getReturnCode()==200){
                                myshupingrecycler.setVisibility(View.VISIBLE);
                                layout.setVisibility(View.GONE);
                                datas.addAll(dataBean.getData().getList());
                                mAdapter.notifyDataSetChanged();
                            }else if(dataBean.getReturnCode()==800&&page==1){
                                myshupingrecycler.setVisibility(View.GONE);
                                layout.setVisibility(View.VISIBLE);
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                        }

                    }
                });

    }


}
